﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="puzzle.aspx.cs" Inherits="puzzle" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ import Namespace="RobozzleModel" %>
<%@ Import Namespace="System.Linq" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>RoboZZle stats for puzzle <%  Response.Write(Request.Params["id"]); %></title>
    <link href="stats.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<%= StatsCommon.MenuBar(MenuPage.None)%>

    <%
        int puzzleId;
        bool good = true;
        if (!int.TryParse(Request.Params["id"], out puzzleId)) good = false;
         
        RobozzleModel.RobozzleEntities entities = null;
        LevelWithDifficulty level = null;
        
        if (good) {
            entities = new RobozzleModel.RobozzleEntities();
            level = entities.LevelWithDifficulty.Where(L => L.Id == puzzleId).FirstOrDefault();
        }
        if (level == null) good = false;
        
        if (!good) {
            Response.Write(string.Format("Unknown puzzle ID.", puzzleId));
        }
        else {
            int liked = (level.CoolnessVoteSum + level.CoolnessVoteCount) / 2;
            int disliked = level.CoolnessVoteCount - liked;
            string difficulty = level.DifficultyVoteCount > 0 
                ? String.Format("{0:0.00}",(1.0*level.DifficultyVoteSum / level.DifficultyVoteCount))
                : "?";
            string authorHtml = level.SubmittedBy == null
                ? "<i>Anonymous</i>"
                : string.Format("<a href='user.aspx?name={0}'>{0}</a>", level.SubmittedBy);

            string STR_AT_REGISTRATION = "#at registration#";
            int specialVal = 100000;
            var solvers = entities.Solution.Where(S => S.LevelId == puzzleId).Where(S => S.UserName != null).GroupBy(S => S.UserName)
                .Select(G => new { user = G.Key, minLength = G.Min(S => (S.Solution1 == null || S.Solution1 == STR_AT_REGISTRATION) ? specialVal : (S.Solution1.Length - 5) / 2) }).ToArray();

            int commentCount = 0;
            var thread = entities.ForumThread.Where(t => t.PuzzleId == puzzleId).FirstOrDefault();
            if (thread != null)
            {
                if (!thread.ForumComment.IsLoaded) thread.ForumComment.Load();
                commentCount = thread.ForumComment.Count();
            }
            
            string clientTextState;
            try
            {
                clientTextState = ToClientTextState(level);
            }
            catch (Exception)
            {
                // Creating the designer link failed. Just continue.
                clientTextState = "";
            }
    %>
    <h1>Puzzle <% Response.Write(puzzleId); %>: <% Response.Write(Server.HtmlEncode(level.Title)); %></h1>
    <p><% Response.Write(Server.HtmlEncode(level.About)); %></p>
    <div style="margin-left: auto; margin-right: auto; width: 320px;">
        <a href="index.aspx?puzzle=<%=level.Id %>">
            <img src="images/puzzlethumb/<%= level.Id %>.png" style="border: solid black 1px"/>
        </a>        
    </div>
    <p>
        <a href="index.aspx?puzzle=<% Response.Write(puzzleId); %>">Play</a>
        &nbsp;&nbsp;
        <a href="index.aspx#design/<%= clientTextState %>">Designer template</a>
        &nbsp;&nbsp;
        <a href="forums/thread.aspx?puzzle=<%= level.Id %>"><%= StatsCommon.Plural("comment", commentCount) %></a>
    </p>
    <table border="0">
    <tr><td><strong>Author:</strong></td><td> <% Response.Write(authorHtml); %> </td></tr>
    <tr><td><strong>Date posted:</strong></td><td> <% Response.Write(level.SubmittedDate); %> </td></tr>
    <tr><td><strong>Difficulty (1..5):</strong></td><td> <% Response.Write(string.Format("{0} ({1} votes)", difficulty, level.DifficultyVoteCount)); %></td></tr>
    <tr><td><strong>Popularity:</strong></td><td> <% Response.Write(string.Format("{0} ({1} liked, {2} disliked)", level.CoolnessVoteSum, liked, disliked)); %></td></tr>
    <tr><td><strong>Correct solution submitted:</strong></td><td> <% Response.Write(level.Solutions); %></td></tr>
    </table>
    
    <p>&nbsp;</p>
    <h2>Solved by <% Response.Write(solvers.Length); %> players</h2>
    <table>
    
    <tr>
    <%
        int counter = 0;
        foreach (var solver in solvers.OrderBy(x => x.minLength).ThenBy(x => x.user))
        {
            if (counter > 0 && counter % 5 == 0) Response.Write("</tr><tr>");
            Response.Write(string.Format("<td width='80'><a href='user.aspx?name={0}'>{0}</a> ({1})</td>", solver.user, solver.minLength == specialVal  ? "?" : solver.minLength.ToString()));
            counter++;
        }
    %>  
    </tr>
    </table>       
    <p><small>Number of commands in the solution is shown in brackets. Shown as ? when not available.</small></p>
     <%
         Response.Write(StatsCommon.Footer());
        }
     %>
</body>
</html>
